package cn.lutu.domain.repository.impl;

import cn.lutu.dao.mapper.CategoryMapper;
import cn.lutu.domain.repository.CategoryRepository;
import cn.lutu.protocol.entity.Category;
import cn.lutu.protocol.vo.CategoryListVO;
import cn.lutu.protocol.vo.CategoryStandardVO;
import cn.lutu.util.common.PageInfoToPageDataConverter;
import cn.lutu.util.pojo.vo.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class CategoryRepositoryImpl implements CategoryRepository {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public int insert(Category category) {
        log.info("开始执行新增类别的功能");
        return categoryMapper.insert(category);
    }

    @Override
    public int deleteById(Long id) {
        log.info("开始执行删除类别的功能id为{}",id);
        return categoryMapper.deleteById(id);
    }

    @Override
    public int updateById(Category category) {
        log.info("开始执行更新类别的功能,参数为:{}",category);
        return categoryMapper.updateById(category);
    }

    @Override
    public int selectByName(String name) {
        log.info("开始执行根据名称查询类别的功能，名称为：{}",name);
        QueryWrapper<Category> wrapper = new QueryWrapper();
        wrapper.eq("name",name);
        return categoryMapper.selectCount(wrapper);
    }

    @Override
    public int selectCount(Long parentId) {
        log.info("开始执行parentId统计类别的数量,id为{}",parentId);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        Integer count = categoryMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public CategoryStandardVO selectStandardById(Long id) {
        log.debug("开始执行根据ID查询类别的功能：{}",id);
        return categoryMapper.selectCategoryById(id);
    }

    @Override
    public Category selectById(Long id) {
        log.info("开始执行根据ID查询类别的功能，名称为：{}",id);

        Category category = categoryMapper.selectById(id);
        return category;
    }

    @Override
    public List<CategoryListVO> getCategoryList() {
        log.info("开始执行查询类别列表的功能(生成类别树)");
        return categoryMapper.selectCategories();
    }

    @Override
    public List<CategoryListVO> getCategoryByParentId(Long parentId) {
        log.info("开始执行根据parentId查询类别的功能");

        return categoryMapper.selectByParentId(parentId);
    }

    @Override
    public PageData<CategoryListVO> selectCategories(Integer pageNum, Integer pageSize) {
        log.info("开始执行查询类别列表的功能");
        PageHelper.startPage(pageNum, pageSize);
        List<CategoryListVO> list = categoryMapper.selectCategories();
        PageInfo<CategoryListVO> pageInfo = new PageInfo<>(list);
        PageData<CategoryListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;

    }
}
